package com.liwalo.dao;

// Generated 24-ago-2013 14:46:30 by Hibernate Tools 3.6.0

import static org.hibernate.criterion.Example.create;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.liwalo.entities.Ciudad;
import com.liwalo.entities.Pais;
import com.liwalo.entities.Provincia;
import com.liwalo.utils.HibernateUtil;

/**
 * Home object for domain model class Provincia.
 * @see com.liwalo.dao.Provincia
 * @author Hibernate Tools
 */
public class ProvinciaDAO {

	private static final Log log = LogFactory.getLog(ProvinciaDAO.class);

	private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

	public void persist(Provincia transientInstance) {
		log.debug("persisting Provincia instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Provincia instance) {
		log.debug("attaching dirty Provincia instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Provincia instance) {
		log.debug("attaching clean Provincia instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Provincia persistentInstance) {
		log.debug("deleting Provincia instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Provincia merge(Provincia detachedInstance) {
		log.debug("merging Provincia instance");
		try {
			Provincia result = (Provincia) sessionFactory.getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Provincia findById(java.lang.Integer id) {
		log.debug("getting Provincia instance with id: " + id);
		try {
			Provincia instance = (Provincia) sessionFactory.getCurrentSession().get(
					Provincia.class, id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Provincia> FindByIdProvincia(Provincia instance) {
		log.debug("finding Provincia instance by example");
		try {
			List<Provincia> results = (List<Provincia>) sessionFactory
					.getCurrentSession()
					.createCriteria(Provincia.class)
					.createAlias("paisId", "pais")
					.add(Restrictions.eq("pais.id", instance.getPaisId().getId()))
					.add(Restrictions.eq("estado",1))
					.list();
					
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List<Ciudad> findByExample(Provincia instance) {
		log.debug("finding Ciudad instance by example");
		try {
			List<Ciudad> results = (List<Ciudad>) sessionFactory
					.getCurrentSession()
					.createCriteria("com.liwalo.dao.Pais")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Provincia> getAllByPais(Pais instance) {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Provincia.class)
								   .createAlias("paisId", "pais")
								   .add(Restrictions.eq("pais.id", instance.getId()))
								   .add(Restrictions.eq("estado",1));
		return (List<Provincia>) criteria.list();
	}
}
